let express = require("express")();
let expressStatic = require('express-static');
let mysql = require('mysql');
let fs = require('fs');
let url = require("url")
var sql = mysql.createConnection({
    // 别改 
    host: 'localhost',
    user: 'root',
    password: '123456',
    database: 'school'
});
// 尝试链接
sql.connect();
const port = 80

// 配置读取首页文件
express.get("/", (request, response) => {
    fs.readFile("static/nameList.html", (error, data) => {
        if (error) {
            response.end("error");
            console.log("找不到首页文件了")
        } else {
            response.end(data);
        }
    })
})
// 获取数据库student内容
express.get("/getlist", (request, response) => {
    sql.query(`SELECT * FROM students`, (error, data) => {
        if (!error) {
            response.end(JSON.stringify(data))
        } else {
            response.end("凉");
            console.log("数据库查找失败了")
        }
    })
})

// 删除数据库student内容
express.get("/delete", (request, response) => {
    let id = url.parse(request.url, true).query.id;
    // 判断id值获不获取的到
    if (id === undefined) {
        response.end("请发送要删除的学员id")
    } else {
        sql.query(`DELETE FROM students WHERE id=${id}`, (error, data) => {
            if (!error) {
                response.end("success")
                console.log("ok")
            } else {
                response.end("凉");
                console.log("数据库查找失败了")
            }
        })
    }
})

// 添加数据库student内容
express.get("/add", (request, response) => {
    let name = url.parse(request.url, true).query.name;
    let age = url.parse(request.url, true).query.age;
    let sex = url.parse(request.url, true).query.sex;
    let city = url.parse(request.url, true).query.city;
    sql.query(`INSERT INTO students (name,age,sex,city) VALUES ("${name}","${age}","${sex}","${city}")`, (error, data) => {
        if (!error) {
            response.end("success")
        } else {
            response.end("凉");
            console.log("数据库添加失败了")
        }
    })
})

// 修改数据库student内容
express.get("/change", (request, response) => {
    let name = url.parse(request.url, true).query.name;
    let age = url.parse(request.url, true).query.age;
    let sex = url.parse(request.url, true).query.sex;
    let city = url.parse(request.url, true).query.city;
    let id = url.parse(request.url, true).query.id;
    sql.query(`UPDATE students SET name="${name}",age="${age}",sex="${sex}",city="${city}" WHERE id="${id}"`, (error, data) => {
        if (!error) {
            response.end("success")
        } else {
            response.end("凉");
            console.log("数据库添加失败了")
            console.log(error)
        }
    })
})


//注册添加userlist列表
express.get("/rgr", (request, response) => {
    let username = url.parse(request.url, true).query.username;
    let password = url.parse(request.url, true).query.password;
    sql.query(`INSERT INTO userlist (username,password) VALUES ("${username}","${password}")`, (error, data) => {
        if (!error) {
            response.end("success")
        } else {
            response.end("凉");
            console.log("数据库添加失败了")
        }
    })
})

// 登录
express.get("/log", (request, response) => {
    let username = url.parse(request.url, true).query.username;
    let password = url.parse(request.url, true).query.password;
    sql.query(`SELECT * FROM userlist WHERE username=${username} AND password=${password}`, (error, data) => {
        if (!error) {
            response.end("success")
        } else {
            response.end("凉");
            console.log("登录失败")
        }
    })
})

express.use(expressStatic(`${__dirname}/static`))
// 开始监听来自前端的请求，也叫启动服务
// 这个操作不会停止
express.listen(port, () => {
    console.log(`server is running at ${port}`)
})